<?php
class ModelMenuFood extends Model {
	public function addFood($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "food SET restaurant_id = '" . (int)$this->restaurant->getRestaurantId() . "', top = '" . (int)$data['top'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
		
		$food_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "food SET image = '" . $this->db->escape($data['image']) . "' WHERE food_id = '" . (int)$food_id . "'");
		}
		
		foreach ($data['food_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "food_description SET food_id = '" . (int)$food_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		if (isset($data['food_attribute'])) {
			foreach ($data['food_attribute'] as $food_attribute) {
				if ($food_attribute['attribute_id']) {
					$this->db->query("DELETE FROM " . DB_PREFIX . "food_attribute WHERE food_id = '" . (int)$food_id . "' AND attribute_id = '" . (int)$food_attribute['attribute_id'] . "'");
					
					foreach ($food_attribute['food_attribute_description'] as $language_id => $food_attribute_description) {				
						$this->db->query("INSERT INTO " . DB_PREFIX . "food_attribute SET food_id = '" . (int)$food_id . "', attribute_id = '" . (int)$food_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" .  $this->db->escape($food_attribute_description['text']) . "'");
					}
				}
			}
		}
	
		if (isset($data['food_option'])) {
			foreach ($data['food_option'] as $food_option) {
				if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') {
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_option SET food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', required = '" . (int)$food_option['required'] . "'");
				
					$food_option_id = $this->db->getLastId();
				
					if (isset($food_option['food_option_value'])) {
						foreach ($food_option['food_option_value'] as $food_option_value) {
							$this->db->query("INSERT INTO " . DB_PREFIX . "food_option_value SET food_option_id = '" . (int)$food_option_id . "', food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', option_value_id = '" . $this->db->escape($food_option_value['option_value_id']) . "', price = '" . (float)$food_option_value['price'] . "', price_prefix = '" . $this->db->escape($food_option_value['price_prefix']) . "'");
						} 
					}
				} else { 
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_option SET food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', option_value = '" . $this->db->escape($food_option['option_value']) . "', required = '" . (int)$food_option['required'] . "'");
				}
			}
		}

		if (isset($data['food_special'])) {
			foreach ($data['food_special'] as $food_special) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_special SET food_id = '" . (int)$food_id . "', priority = '" . (int)$food_special['priority'] . "', price = '" . (float)$food_special['price'] . "', date_start = STR_TO_DATE('" . $this->db->escape($food_special['date_start']) . "','%d/%m/%Y'), date_end = STR_TO_DATE('" . $this->db->escape($food_special['date_end']) . "','%d/%m/%Y')");
			}
		}
		
		if (isset($data['food_image'])) {
			foreach ($data['food_image'] as $food_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_image SET food_id = '" . (int)$food_id . "', image = '" . $this->db->escape($food_image['image']) . "', sort_order = '" . (int)$food_image['sort_order'] . "'");
			}
		}
		
		if (isset($data['food_menu'])) {
			foreach ($data['food_menu'] as $menu_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_to_menu SET food_id = '" . (int)$food_id . "', menu_id = '" . (int)$menu_id . "'");
			}
		}
		
		foreach ($data['food_tag'] as $language_id => $value) {
			if ($value) {
				$tags = explode(',', $value);
				
				foreach ($tags as $tag) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_tag SET restaurant_id = '" . (int)$restaurant_id . "', food_id = '" . (int)$food_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
				}
			}
		}

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'food_id=" . (int)$food_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('food');
	}
	
	public function editFood($food_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "food SET top = '" . (int)$data['top'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE food_id = '" . (int)$food_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "food SET image = '" . $this->db->escape($data['image']) . "' WHERE food_id = '" . (int)$food_id . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_description WHERE food_id = '" . (int)$food_id . "'");
		
		foreach ($data['food_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "food_description SET food_id = '" . (int)$food_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
	
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_attribute WHERE food_id = '" . (int)$food_id . "'");

		if (isset($data['food_attribute'])) {
			foreach ($data['food_attribute'] as $food_attribute) {
				if ($food_attribute['attribute_id']) {
					$this->db->query("DELETE FROM " . DB_PREFIX . "food_attribute WHERE food_id = '" . (int)$food_id . "' AND attribute_id = '" . (int)$food_attribute['attribute_id'] . "'");
					
					foreach ($food_attribute['food_attribute_description'] as $language_id => $food_attribute_description) {				
						$this->db->query("INSERT INTO " . DB_PREFIX . "food_attribute SET food_id = '" . (int)$food_id . "', attribute_id = '" . (int)$food_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" .  $this->db->escape($food_attribute_description['text']) . "'");
					}
				}
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "food_option WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_option_value WHERE food_id = '" . (int)$food_id . "'");
		
		if (isset($data['food_option'])) {
			foreach ($data['food_option'] as $food_option) {
				if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') {
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_option SET food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', required = '" . (int)$food_option['required'] . "'");
				
					$food_option_id = $this->db->getLastId();
				
					if (isset($food_option['food_option_value'])) {
						foreach ($food_option['food_option_value'] as $food_option_value) {
							$this->db->query("INSERT INTO " . DB_PREFIX . "food_option_value SET food_option_id = '" . (int)$food_option_id . "', food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', option_value_id = '" . $this->db->escape($food_option_value['option_value_id']) . "', price = '" . (float)$food_option_value['price'] . "', price_prefix = '" . $this->db->escape($food_option_value['price_prefix']) . "'");
						} 
					}
				} else { 
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_option SET food_id = '" . (int)$food_id . "', option_id = '" . (int)$food_option['option_id'] . "', option_value = '" . $this->db->escape($food_option['option_value']) . "', required = '" . (int)$food_option['required'] . "'");
				}
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_special WHERE food_id = '" . (int)$food_id . "'");
		
		if (isset($data['food_special'])) {
			foreach ($data['food_special'] as $food_special) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_special SET food_id = '" . (int)$food_id . "', priority = '" . (int)$food_special['priority'] . "', price = '" . (float)$food_special['price'] . "', date_start = STR_TO_DATE('" . $this->db->escape($food_special['date_start']) . "','%d/%m/%Y'), date_end = STR_TO_DATE('" . $this->db->escape($food_special['date_end']) . "','%d/%m/%Y')");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_image WHERE food_id = '" . (int)$food_id . "'");
		
		if (isset($data['food_image'])) {
			foreach ($data['food_image'] as $food_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_image SET food_id = '" . (int)$food_id . "', image = '" . $this->db->escape($food_image['image']) . "', sort_order = '" . (int)$food_image['sort_order'] . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_to_menu WHERE food_id = '" . (int)$food_id . "'");
		
		if (isset($data['food_menu'])) {
			foreach ($data['food_menu'] as $menu_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "food_to_menu SET food_id = '" . (int)$food_id . "', menu_id = '" . (int)$menu_id . "'");
			}		
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_tag WHERE food_id = '" . (int)$food_id. "'");
		
		foreach ($data['food_tag'] as $language_id => $value) {
			if ($value) {
				$tags = explode(',', $value);
			
				foreach ($tags as $tag) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "food_tag SET food_id = '" . (int)$food_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
				}
			}
		}
						
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'food_id=" . (int)$food_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'food_id=" . (int)$food_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
						
		$this->cache->delete('food');
	}
	
	public function deleteFood($food_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "food WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_attribute WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_description WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_image WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_option WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_option_value WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_special WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_tag WHERE food_id='" . (int)$food_id. "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_to_menu WHERE food_id = '" . (int)$food_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "food_review WHERE food_id = '" . (int)$food_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'food_id=" . (int)$food_id. "'");
		
		$this->cache->delete('food');
	}
	
	public function getFood($food_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'food_id=" . (int)$food_id . "') AS keyword FROM " . DB_PREFIX . "food f LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id) WHERE f.food_id = '" . (int)$food_id . "' AND fd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->row;
	}
	
	public function getFoods($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "food f LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id)";
			
			if (!empty($data['filter_menu']) || !empty($data['filter_menu_id'])) {
				$sql .= " INNER JOIN " . DB_PREFIX . "food_to_menu f2m ON (f.food_id = f2m.food_id)";			
			}
					
			$sql .= " WHERE " .
			        " f.restaurant_id = '" . $this->restaurant->getRestaurantId() . "'" . 
					" AND fd.language_id = '" . (int)$this->config->get('config_language_id') . "'"; 
			
			if (!empty($data['filter_menu'])) {
				$sql .= " AND LCASE(f2m.menu_id) = '" . (int)$data['filter_menu'] . "'";
			}
			
			if (!empty($data['filter_name'])) {
				$sql .= " AND LCASE(fd.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
			}
			
			if (!empty($data['filter_price'])) {
				$sql .= " AND f.price LIKE '" . $this->db->escape($data['filter_price']) . "%'";
			}
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND f.status = '" . (int)$data['filter_status'] . "'";
			}
					
			if (!empty($data['filter_menu_id'])) {
				if (!empty($data['filter_sub_menu'])) {
					$implode_data = array();
					
					$implode_data[] = "menu_id = '" . (int)$data['filter_menu_id'] . "'";
					
					$this->load->model('menu/menu');
					
					$menus = $this->model_menu_menu->getMenus($data['filter_menu_id']);
					
					foreach ($menus as $menu) {
						$implode_data[] = "f2m.menu_id = '" . (int)$menu['menu_id'] . "'";
					}
					
					$sql .= " AND (" . implode(' OR ', $implode_data) . ")";			
				} else {
					$sql .= " AND f2m.menu_id = '" . (int)$data['filter_menu_id'] . "'";
				}
			}
			
			$sql .= " GROUP BY f.food_id";
						
			$sort_data = array(
				'fd.name',
				'f.price',
				'f.status',
				'f.sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY fd.name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$food_data = $this->cache->get('food.' . (int)$this->config->get('config_language_id') . '.' . $this->restaurant->getRestaurantId());
		
			if (!$food_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food f LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id) WHERE restaurant_id = '" . $this->restaurant->getRestaurantId() . "' AND fd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY fd.name ASC");
	
				$food_data = $query->rows;
			
				$this->cache->set('food.' . (int)$this->config->get('config_language_id'), $food_data);
			}	
	
			return $food_data;
		}
	}
	
	public function getFoodsByMenuId($menu_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food f LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id) LEFT JOIN " . DB_PREFIX . "food_to_menu f2m ON (f.food_id = f2m.food_id) WHERE fd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND f2m.menu_id = '" . (int)$menu_id . "' ORDER BY fd.name ASC");
								  
		return $query->rows;
	} 
	
	public function getFoodDescriptions($food_id) {
		$food_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_description WHERE food_id = '" . (int)$food_id . "'");
		
		foreach ($query->rows as $result) {
			$food_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'description'      => $result['description'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description']
			);
		}
		
		return $food_description_data;
	}

	public function getFoodAttributes($food_id) {
		$food_attribute_data = array();
		
		$food_attribute_query = $this->db->query("SELECT pa.attribute_id, ad.name FROM " . DB_PREFIX . "food_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.food_id = '" . (int)$food_id . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY pa.attribute_id");
		
		foreach ($food_attribute_query->rows as $food_attribute) {
			$food_attribute_description_data = array();
			
			$food_attribute_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_attribute WHERE food_id = '" . (int)$food_id . "' AND attribute_id = '" . (int)$food_attribute['attribute_id'] . "'");
			
			foreach ($food_attribute_description_query->rows as $food_attribute_description) {
				$food_attribute_description_data[$food_attribute_description['language_id']] = array('text' => $food_attribute_description['text']);
			}
			
			$food_attribute_data[] = array(
				'attribute_id'                  => $food_attribute['attribute_id'],
				'name'                          => $food_attribute['name'],
				'food_attribute_description' => $food_attribute_description_data
			);
		}
		
		return $food_attribute_data;
	}
	
	public function getFoodOptions($food_id) {
		$food_option_data = array();
		
		$food_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.food_id = '" . (int)$food_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		foreach ($food_option_query->rows as $food_option) {
			if ($food_option['type'] == 'select' || $food_option['type'] == 'radio' || $food_option['type'] == 'checkbox' || $food_option['type'] == 'image') {
				$food_option_value_data = array();	
				
				$food_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.food_option_id = '" . (int)$food_option['food_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
				foreach ($food_option_value_query->rows as $food_option_value) {
					$food_option_value_data[] = array(
						'food_option_value_id' => $food_option_value['food_option_value_id'],
						'option_value_id'         => $food_option_value['option_value_id'],
						'name'                    => $food_option_value['name'],
						'image'                   => $food_option_value['image'],
						'price'                   => $food_option_value['price'],
						'price_prefix'            => $food_option_value['price_prefix']
					);
				}
				
				$food_option_data[] = array(
					'food_option_id'    => $food_option['food_option_id'],
					'option_id'            => $food_option['option_id'],
					'name'                 => $food_option['name'],
					'type'                 => $food_option['type'],
					'food_option_value'    => $food_option_value_data,
					'required'             => $food_option['required']
				);				
			} else {
				$food_option_data[] = array(
					'food_option_id' => $food_option['food_option_id'],
					'option_id'         => $food_option['option_id'],
					'name'              => $food_option['name'],
					'type'              => $food_option['type'],
					'option_value'      => $food_option['option_value'],
					'required'          => $food_option['required']
				);				
			}
		}	
		
		return $food_option_data;
	}
	
	public function getFoodImages($food_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_image WHERE food_id = '" . (int)$food_id . "'");
		
		return $query->rows;
	}

	public function getFoodSpecials($food_id) {
		$query = $this->db->query("SELECT *, DATE_FORMAT(date_start, '%d/%m/%Y') AS date_start, DATE_FORMAT(date_end, '%d/%m/%Y') AS date_end FROM " . DB_PREFIX . "food_special WHERE food_id = '" . (int)$food_id . "' ORDER BY priority, price");
		
		return $query->rows;
	}
		
	public function getFoodMenus($food_id) {
		$food_menu_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_to_menu WHERE food_id = '" . (int)$food_id . "'");
		
		foreach ($query->rows as $result) {
			$food_menu_data[] = $result['menu_id'];
		}

		return $food_menu_data;
	}
	
	public function getFoodTags($food_id) {
		$food_tag_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "food_tag WHERE food_id = '" . (int)$food_id . "'");
		
		$tag_data = array();
		
		foreach ($query->rows as $result) {
			$tag_data[$result['language_id']][] = $result['tag'];
		}
		
		foreach ($tag_data as $language => $tags) {
			$food_tag_data[$language] = implode(',', $tags);
		}
		
		return $food_tag_data;
	}
	
	public function getTotalFoods($data = array()) {
		$sql = "SELECT COUNT(DISTINCT f.food_id) AS total FROM " . DB_PREFIX . "food f LEFT JOIN " . DB_PREFIX . "food_description fd ON (f.food_id = fd.food_id)";

		if (!empty($data['filter_menu'])) {
			$sql .= " INNER JOIN " . DB_PREFIX . "food_to_menu f2m ON (f.food_id = f2m.food_id)";			
		}
		 
		$sql .= " WHERE fd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
	
		if (!empty($data['filter_menu'])) {
			$sql .= " AND LCASE(f2m.menu_id) = '" . (int)$data['filter_menu'] . "'";
		}
			 			
		if (!empty($data['filter_name'])) {
			$sql .= " AND LCASE(fd.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		
		if (!empty($data['filter_price'])) {
			$sql .= " AND f.price LIKE '" . $this->db->escape($data['filter_price']) . "%'";
		}
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND f.status = '" . (int)$data['filter_status'] . "'";
		}

		if (!empty($data['filter_menu_id'])) {
			if (!empty($data['filter_sub_menu'])) {
				$implode_data = array();
				
				$implode_data[] = "f2m.menu_id = '" . (int)$data['filter_menu_id'] . "'";
				
				$this->load->model('menu/menu');
				
				$menus = $this->model_menu_menu->getMenus($data['filter_menu_id']);
				
				foreach ($menus as $menu) {
					$implode_data[] = "f2m.menu_id = '" . (int)$menu['menu_id'] . "'";
				}
				
				$sql .= " AND (" . implode(' OR ', $implode_data) . ")";			
			} else {
				$sql .= " AND f2m.menu_id = '" . (int)$data['filter_menu_id'] . "'";
			}
		}
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}	
	
	public function getTotalFoodsByAttributeId($attribute_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "food_attribute WHERE attribute_id = '" . (int)$attribute_id . "'");

		return $query->row['total'];
	}	
	
	public function getTotalFoodsByOptionId($option_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "food_option WHERE option_id = '" . (int)$option_id . "'");

		return $query->row['total'];
	}	
}
?>